import {Promise} from 'es6-promise';
import express = require('express');
let iconv = require('iconv-lite');
import { ObjectID } from "mongodb";
import PWSApp from "../../core/app";
import {Request,Response} from "express";
import {
    RouteType,
    PayType,
    TransType,
    TransStatus,
    UserCertStatus
} from "../../core/defines";
import HomeBaseAction from "./act.base";
import {Magics} from "../../core/magics";
import * as utils from "../../core/utils";
import {MongoColls} from "../../config/mongo";
import {ErrCode} from "../../core/error.code";
import UserModel from "../../models/mod.user";
import RegionModel from "../../models/mod.region";
import ProdModel from "../../models/mod.product";

export default class CenterAction extends HomeBaseAction {
    private _userMod: UserModel;
    private _prodMod: ProdModel;
    private _regionMod: RegionModel;

    constructor(app: PWSApp) {
        super(app);
        this._userMod = new UserModel( this.db.collection(MongoColls.USER) );
        this._prodMod = new ProdModel( this.db.collection(MongoColls.PRODUCT) );
        this._regionMod = new RegionModel( this.db.collection(MongoColls.OTHER_PRC_REGION) );
    }

    name() { return `${this.app.name}CenterAction`; }

    protected _setRoutes() {
        this._setRoot('api/center');
        this._route(RouteType.GET, '/', this._actIndex);
        this._route(RouteType.GET, '/order', this._actOrder);
    }

    private _actIndex(req: Request, res: Response) {
        let user = req.session[Magics.SessUser] || {};
        res.render('home/center.html', {user});
    }

    private _actOrder(req: Request, res: Response) {
        Promise.resolve().then( () => {
            return ;
        }).then(() => {
            res.send({err_code: ErrCode.Success});
        }).catch( ( err ) => {
            this._errHandler( req , res , err );
        } );
    }
}
